<?

include '../includes/specs_functions.php';
include "../../includes/feed_class.php";
#------------------------------------------------------------------------
# Connect to PADS
$DB = getDB();
#-------------------------------$Session information
$SESSION = get_session($DB);
$CICUID = $SESSION->Character;
$THEME  = $SESSION->Theme;
$NAME = $SESSION->GetCharacter('name');
$AVATAR = $SESSION->GetCharacter('avatar');
#---------------------------------------------------

$specId = $_POST['specId'];$jobId = $_POST['jobId'];
$layout = $_POST['layout'];
$description = $_POST['description'];

$level = $_POST['level'];$stat = array("status" => '0',  "message" => 'ERROR');
if($CICUID != "" && isset($_POST['level']) && isset($_POST['description']) && isset($_POST['layout']) && isset($_POST['jobId'])){
	if($_POST['layout'] != ""){		$skillArray = explode(";", $_POST['layout']);
	}else{
		$skillArray = array();
	}
	
	if(isset($_POST['specId']) && $_POST['specId'] != "" && $_POST['specId'] != -1){		/* UPDATE */				$table = SPEC_TBL_SPEC;		$data = array(
						SPEC_TBL_SPEC_DESCRIPTION ."= \"".$description."\""
						,SPEC_TBL_SPEC_LEVEL ."= \"".$level."\""
						,SPEC_TBL_SPEC_JOB_ID ."= \"".$jobId."\""
					);		$where = array(
						SPEC_TBL_SPEC_ID ."= \"".$specId."\""
						,SPEC_TBL_SPEC_USER_ID ."= \"".$CICUID."\""
					);				$result = $DB->Update($table ,$data ,$where);				if ($result[0][1] == 0){
			//No error
			$table = SPEC_TBL_COMPONENTS;
			$where = array(
						SPEC_TBL_COMPONENTS_SPEC_ID ."= \"".$specId."\""
					);
			$result = $DB->Remove($table ,$where);
			
			foreach ($skillArray as $skill) {
			
				$skillDetail = explode(":",$skill);
				
				$S_table = SPEC_TBL_COMPONENTS;
				$S_data = array(
									SPEC_TBL_COMPONENTS_SPEC_ID ."= \"".$specId."\"" 
									,SPEC_TBL_COMPONENTS_SKILL_ID ."= \"".$skillDetail[0]."\"" 
									,SPEC_TBL_COMPONENTS_POSITION ."= \"".$skillDetail[1]."\"" 
									,SPEC_TBL_COMPONENTS_TEMPLATE ."= \"".$skillDetail[2]."\"" 
								);
						
				$result = $DB->Insert($S_table ,$S_data);
			}
			$stat = array("status" => '1',  "message" => 'UPDATE');
			
			
			$Feed = new Feed($DB);
			$Message = '<strong><a href="?'. $CICUID .'">'. $NAME .'</a></strong> Updated their <a href="?Specs&'. $specId .'">'.GetClass($jobId).'</a> spec.';
			$Feed->Add($CICUID, $NAME, $AVATAR, 's_update', $Message);
			}
	}else{		/* INSERT */		$table = SPEC_TBL_SPEC;		$col = array(
						SPEC_TBL_SPEC_ID." as specId"
					);		$where = array(
						SPEC_TBL_SPEC_USER_ID ."= \"".$CICUID."\""
					);		$orderBy = SPEC_TBL_SPEC_ID;		$dir = "ASC";				$result = $DB->GetData($table ,$col ,true ,$where, $orderBy, $dir, null);				if($result[0] < 7){

			$table = SPEC_TBL_SPEC;			$data = array(							SPEC_TBL_SPEC_DESCRIPTION ."= \"".$description."\""							,SPEC_TBL_SPEC_LEVEL ."= \"".$level."\"" 							,SPEC_TBL_SPEC_CDATE ."= \"".date('Y-m-d H:i:s')."\""							,SPEC_TBL_SPEC_UDATE ."= \"".date('Y-m-d H:i:s')."\""							,SPEC_TBL_SPEC_USER_ID ."= \"".$CICUID."\""							,SPEC_TBL_SPEC_JOB_ID ."= \"".$jobId."\""
							,SPEC_TBL_SPEC_PUBLIC ."= \"N\""
						);							$result = $DB->Insert($table ,$data);				
						if ($result[0][1] == 0)
			{				$new_sId = $result[1];
				
				foreach ($skillArray as $skill) {
					$skillDetail = explode(":",$skill);
					
					$S_table = SPEC_TBL_COMPONENTS;
					$S_data = array(
									SPEC_TBL_COMPONENTS_SPEC_ID ."= \"".$new_sId."\"" 
									,SPEC_TBL_COMPONENTS_SKILL_ID ."= \"".$skillDetail[0]."\"" 
									,SPEC_TBL_COMPONENTS_POSITION ."= \"".$skillDetail[1]."\"" 
									,SPEC_TBL_COMPONENTS_TEMPLATE ."= \"".$skillDetail[2]."\"" 
								);
						
					$result = $DB->Insert($S_table ,$S_data);
				}
				
				$Feed = new Feed($DB);
				$Message = '<strong><a href="?'. $CICUID .'">'. $NAME .'</a></strong> Created a new <a href="?Specs&'. $new_sId .'">'.GetClass($jobId).'</a> spec.';
				$Feed->Add($CICUID, $NAME, $AVATAR, 's_insert', $Message);				$stat = array("status" => '1',  "message" => 'INSERT', "id" => $new_sId);			}		}else{
			$stat = array("status" => '0',  "message" => 'TOOMANY');		}
	}
}
echo json_encode($stat);
#------------------------------------------------------------------------# DisconnectleaveDB($DB);
?>